


/***
 *      基于事件驱动的多任务操作系统
 *      
 *          低内存和多任务系统
 *              有优先级
 * 
 *          采用事件驱动任务   -->  轮询方式
 * 
 * 
 *          
 * 
 */


/***
 *          任务 （task)
 * 
 *              优先级高的运行
 */



#include <stdint.h>


typedef enum
{
    result_ok = 0,    /* os 成功*/
    result_error,     /* os 失败 */
    result_max
}os_result_t;

typedef enum
{
    BIT_NO  = 0x0000, 
    BIT_0   = 0x0001,
    BIT_1   = 0x0002,
    BIT_2   = 0x0004,
    BIT_3   = 0x0008,
    BIT_4   = 0x0010,
    BIT_5   = 0x0020,
    BIT_6   = 0x0030,
    BIT_7   = 0x0040,
    BIT_8   = 0x0100,
    BIT_9   = 0x0200,
    BIT_10  = 0x0300,
    BIT_11  = 0x0400,
    BIT_12  = 0x1000,
    BIT_13  = 0x2400,
    BIT_14  = 0x4000,
    BIT_15  = 0x8000,
    BIT_ANY = 0xFFFF,
}os_event_t;


#define SET_BIT(num,bit) (num) |=(1 << (bit)) 
#define RESET_BIT(num,bit) (num) &=~(1 << (bit)) 


/**
 *  @brief   任务    
 *      
 *      list:
 *           next
 *      
 *      private:
 *          
 *          task_id
 *          event
 *          askPriority 
 * 
 *      pos
 *          init()
 *      
 *          handle_fun()  
 *          
 *      
 *      // 事件的操作函数
 *      os_set_event(task_id  event)  --> result_t
 *      os_clear_event(task_id event) --> result_t
 * 
 *      // task函数
 *      add_task(os_task_t task)  -->   result_t  ( task_id  priorty  ops*);
 *      del_self()--> result_t
 *      del_task(task_id_t task_t)-->   result_t 
 *      
 *      // 查找
 *      find_next_active() -- >         os_task_t
 *      find_is_exist(task_id_t task) --> os_bool_t
 *        
 * 
 */

typedef  uint16_t task_id_t;
typedef  uint8_t  priority_t;
typedef  bool     os_bool_t;

typedef void(*init_t)(task_id_t task_id);
typedef void(*handle_fun_t)(task_id_t task_id,os_event_t event);


typedef struct
{
    os_task_t*      next;        

    task_id_t       task_id;          
    priority_t      task_priority;
    os_event_t      event;

    init_t          init;
    handle_fun_t    handle;

}os_task_t;



/** -------------------------------------- */

typedef struct 
{
    
    static os_task_t* task_head;            /* 任务列表  */

    static uint16_t   task_count;             /* 当前任务数量 */

    static os_task_t* active_task;          /* 当先活跃任务*/

}_os_task_t;



/*  优先级高的 添加在优先级低的前面  */
os_result_t os_set_event(task_id_t  task_id,os_event_t event);
os_result_t os_clear_event(task_id_t  task_id,os_event_t event);
/*  优先级高的 添加在优先级低的前面  任务具有一个*/
os_result_t os_add_task(os_task_t* task);
/*  */
os_result_t os_del_self(void);
os_result_t os_del_task(task_id_t  task_id);

os_task_t os_find_next_active(void);
os_bool_t os_find_exist(task_id_t  task_id);


void os_system_init(void);
void os_system_start(void);




/***
 * 
 *          timer   -->
 *              
 *              超时时间到了  --> 发送事件通知 任务
 *              
 *              销毁timer
 *                  单次 周期性的
 * 
 *          timer   
 *              list：
 *                  next
 *              
 *              dest_id
 *              event
 *              
 *              timeout
 *              reload_time
 *         // 添加软件定时器     
 *         os_timer_add_single  (task_id_t  task_id,os_event_t event,os_time_t timeout) --> os_result_t
 *         os_timer_add_cycle   (task_id_t  task_id,os_event_t event,os_time_t timeout) --> os_result_t
 *         // 基本操作
 *         os_timer_stop(task_id_t  task_id,os_event_t event)  --> os_result_t
 *         os_timer_del(task_id_t  task_id,os_event_t event)  --> os_result_t
 *         os_timer_get_systick()           -->os_systick_t
 *         os_timer_get_residue_timeout()   -->timeout_t
 * 
 *         void os_timer_init(void);
 *         void os_timer_start(void);
 * 
 */

typedef uint16_t os_timeout_t;
typedef uint32_t os_systick_t;



typedef struct
{
    os_timer_t* next;

    task_id_t       dest_id;
    os_timeout_t    timeout;
    os_timeout_t    reload_time;
    os_event_t      event;

}os_timer_t;



os_result_t os_timer_add_single (task_id_t  task_id,os_event_t event,os_timeout_t timeout);
os_result_t os_timer_add_cycle (task_id_t  task_id,os_event_t event,os_timeout_t timeout);

os_result_t os_timer_stop(task_id_t  task_id,os_event_t event);
/* event  = BIT_NO   等待下一轮来删除 */
os_result_t os_timer_del(task_id_t  task_id, os_event_t event);

os_systick_t os_timer_get_systick(void);

os_timeout_t os_timer_get_residue_timeout(task_id_t  task_id,os_event_t event);


void os_timer_init(void);
void os_timer_start(void);






/**  
 *      msg 
 *          
 *          异步非阻塞发送消息给任务
 * 
 *      list:
 *          next
 * 
 *          dest_id;
 *          user_data
 *          data_len
 * 
 *      os_msg_send(task_id_t  task_id,void* user_data,os_data_len_t data_len ) --> os_result_t
 * 
 * 
 *      add_msg_alloc() --> os
 * 
 *  
 */





